# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os
import re
from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import rm, copy, apply_conandata_patches, export_conandata_patches
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout, CMakeDeps

required_conan_version = ">=2.13.0"


class LibSPDMConan(ConanFile):
    name = "libspdm"
    license = "BSD-3-Clause"
    description = "Protocol buffer schemas for OpenTelemetry"
    settings = "os", "arch", "compiler", "build_type"
    exports_sources = ["permissions.ini"]
    options = {
        "shared": [True, False],
        "with_abseil": [True, False],
        "with_otlp": [True, False],
        "with_otlp_grpc": [True, False],
        "with_zipkin": [True, False],
        "with_prometheus": [True, False],
        "with_elasticsearch": [True, False]
    }
    default_options = {
        "shared": True,
        "with_abseil": True,
        "with_otlp": False,
        "with_otlp_grpc": True,
        "with_zipkin": False,
        "with_prometheus": False,
        "with_elasticsearch": False
    }

    def layout(self):
        cmake_layout(self, src_folder="src")

    def requirements(self):
        requires = self.conan_data.get("requires", {}).get(self.version, {})
        for req in requires:
            self.requires(req)

    def source(self):
        print("============================== start to install source - libspdm ==============================")
        source = self.conan_data["sources"][self.version]
        git = Git(self)
        git.fetch_commit(url=source["url"], commit=source["branch"])

        copy(self, "*.h", src=self.dependencies["openssl"].cpp_info.includedirs[0], dst=self.source_folder + "/os_stub/openssllib/openssl")

    def export_sources(self):
        export_conandata_patches(self)
        
    def build(self):
        apply_conandata_patches(self)
        print("============================== start to build ==============================")
        cmake = CMake(self)
        cmake.configure(cli_args=["--no-warn-unused-cli"])
        cmake.build()

    def generate(self):
        print("============================== start to generate ==============================")
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)

        if self.settings.arch == "x86_64":
            tc.variables["ARCH"] = "x64"
            tc.variables["TOOLCHAIN"] = "GCC"
        else:
            tc.variables["ARCH"] = "aarch64"
            tc.variables["TOOLCHAIN"] = "AARCH64_GCC"

        if self.settings.build_type == "Release":
            tc.variables["TARGET"] = "Release"
        else:
            tc.variables["TARGET"] = "Debug"

        tc.variables["CRYPTO"] = "openssl"
        tc.variables["BUILD_LINUX_SHARED_LIB"] = "ON"
        tc.variables["ENABLE_CODEQL"] = "OFF"
        tc.variables["DISABLE_TESTS"] = "1"
        tc.variables["CMAKE_VERBOSE_MAKEFILE"] = "ON"

        tc.variables["ENABLE_BINARY_BUILD"] = "1"
        tc.variables["COMPILED_LIBCRYPTO_PATH"] = self.dependencies["openssl"].package_folder + "/usr/lib64/spdm_ssl/libcrypto.so.3"
        tc.variables["COMPILED_LIBSSL_PATH"] = self.dependencies["openssl"].package_folder + "/usr/lib64/spdm_ssl/libssl.so.3"

        tc.generate()

    def package(self):
        copy(self, "*", src=self.source_folder + "/include", dst=self.package_folder + "/include")
        copy(self, "*.so", src=self.build_folder + "/lib", dst=self.package_folder + "/usr/lib64")

    def package_info(self):
        self.cpp_info.includedirs = ["include"]
        self.cpp_info.libs = ["spdm", "spdm_platform", "spdm_crypto"]
        self.cpp_info.libdirs = ["usr/lib64"]